/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.panels;

import Domain.Materiaal;
import Delta.Lamel;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Iterator;
import laminaatapplicatie.Global;

/**
 *
 * @author Memet
 */
public class LaminateLayerPanel extends javax.swing.JPanel {

    /**
     * Creates new form LaminateLayerPanel
     */
    public LaminateLayerPanel() {
        initComponents();
        updateContent();
    }
    
    public LaminateLayerPanel(int nr, double angle, Lamel lamel, double thickness) {
        initComponents();
        setNumber(nr);
        
        double hoek = angle;
        if(hoek >= -90 || hoek <= 90)
        {
            sldHoek.setValue((int)hoek);
            txthoek.setText(String.valueOf(hoek));
        }
        else
        {
            if(hoek > 90)
            {
                sldHoek.setValue(90);
                txthoek.setText("90");
            }
            else
            {
                sldHoek.setValue(-90);
                txthoek.setText("-90");
            }
        }
        examp.setTheta(sldHoek.getValue());
        updateContent();

        cbMats.setSelectedItem(lamel);
        updateContent();
        txtThickness.setText(String.valueOf(thickness));
        cbMats.setSize(58, 23);
        this.revalidate();
        this.repaint();
    }
    
    public void setNumber(int nr)
    {
        lblNumber.setText(String.valueOf(nr)+".");
    }

    public double getAngle()
    {
        return sldHoek.getValue();
    }

    public boolean isSelected()
    {
        return cbLayer.isSelected();
    }

    public void setSelected(boolean b)
    {
        this.cbLayer.setSelected(b);
    }

    public Lamel getMat()
    {
        return (Lamel) cbMats.getSelectedItem();
    }

    public double getThickness()
    {
        return Double.parseDouble(txtThickness.getText());
    }

    private void updateContent()
    {
        int selection = cbMats.getSelectedIndex();
        cbMats.removeAllItems();
        //oud// Iterator<Materiaal> it = Global.data.getMaterialen();
        Iterator<Lamel> it = Global.data.getLamels();
        while(it.hasNext())
        {
            cbMats.addItem(it.next());
        }
        cbMats.setSelectedIndex(selection);
        Lamel m = getMat();
        examp.setSet(m.getColorSet());
        examp.setSoort(m.getType());
        examp.repaint();
        txtThickness.setText(String.valueOf(m.getThickness()));
    }

    @Override
    public void paint(Graphics g)
    {
        super.paint(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setColor(Color.black);
        g2.drawLine(0, 0, this.getWidth(), 0);
        //g2.drawLine(0, this.getHeight()-1, this.getWidth(), this.getHeight()-1);
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        sldHoek = new javax.swing.JSlider();
        jLabel1 = new javax.swing.JLabel();
        txthoek = new javax.swing.JTextField();
        examp = new Draw.pnlExample();
        txtThickness = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        cbMats = new Components.SteppedComboBox();
        lblNumber = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        cbLayer = new javax.swing.JCheckBox();

        sldHoek.setBackground(new java.awt.Color(255, 255, 255));
        sldHoek.setMaximum(90);
        sldHoek.setMinimum(-90);
        sldHoek.setPaintTicks(true);
        sldHoek.setSnapToTicks(true);
        sldHoek.setValue(0);
        sldHoek.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sldHoekStateChanged(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Rockwell", 1, 11)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel1.setText("Angle [°]");

        txthoek.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txthoek.setText("0");
        txthoek.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txthoekActionPerformed(evt);
            }
        });
        txthoek.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txthoekFocusLost(evt);
            }
        });

        javax.swing.GroupLayout exampLayout = new javax.swing.GroupLayout(examp);
        examp.setLayout(exampLayout);
        exampLayout.setHorizontalGroup(
            exampLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 50, Short.MAX_VALUE)
        );
        exampLayout.setVerticalGroup(
            exampLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 20, Short.MAX_VALUE)
        );

        txtThickness.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtThickness.setText("0");

        jLabel2.setFont(new java.awt.Font("Rockwell", 1, 11)); // NOI18N
        jLabel2.setText("Thickness [mm]");

        jLabel3.setFont(new java.awt.Font("Rockwell", 1, 11)); // NOI18N
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel3.setText("Example");

        cbMats.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbMats.setMaximumSize(new java.awt.Dimension(58, 23));
        cbMats.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
            public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
                cbMatsPopupMenuWillBecomeInvisible(evt);
            }
            public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
            }
        });

        lblNumber.setText("10.");

        jLabel5.setFont(new java.awt.Font("Rockwell", 1, 11)); // NOI18N
        jLabel5.setText("Material");

        cbLayer.setBackground(new java.awt.Color(255, 255, 255));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cbLayer)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblNumber, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(cbMats, javax.swing.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtThickness)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(sldHoek, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(txthoek, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(examp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbMats, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblNumber)))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cbLayer))
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(examp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtThickness, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txthoek, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(sldHoek, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void sldHoekStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sldHoekStateChanged
        txthoek.setText(String.valueOf(sldHoek.getValue()));
        examp.setTheta(sldHoek.getValue());
    }//GEN-LAST:event_sldHoekStateChanged

    private void txthoekActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txthoekActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txthoekActionPerformed

    private void txthoekFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txthoekFocusLost
        double hoek = Double.parseDouble(txthoek.getText());
        if (hoek >= -90 || hoek <= 90) {
            sldHoek.setValue((int) hoek);
        } else {
            if (hoek > 90) {
                sldHoek.setValue(90);
                txthoek.setText("90");
            } else {
                sldHoek.setValue(-90);
                txthoek.setText("-90");
            }

        }
        examp.setTheta(sldHoek.getValue());
    }//GEN-LAST:event_txthoekFocusLost

    private void cbMatsPopupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_cbMatsPopupMenuWillBecomeInvisible
        updateContent();
    }//GEN-LAST:event_cbMatsPopupMenuWillBecomeInvisible

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox cbLayer;
    private Components.SteppedComboBox cbMats;
    private Draw.pnlExample examp;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel lblNumber;
    private javax.swing.JSlider sldHoek;
    private javax.swing.JTextField txtThickness;
    private javax.swing.JTextField txthoek;
    // End of variables declaration//GEN-END:variables
}
